/// This file creates the raw data outputs for Figure 2. The graphs are created 
/// by the file Figures.R. It also contains calculations for Beta values
/// mentioned in the text.

use "Study1_clean.dta", clear

*** Generate data for Figure 2 (Immigration Attitudes for Republicans, Study 1) 

set more off
mat results = J(1,4,0)
local a=1
local b=1
foreach var of varlist r_immR{
reg `var' treat if rep == 1, robust
	mat results[`a',1] = _b[treat]
	mat results[`a',2] = _se[treat]
	mat results[`a',3] = `a'
	mat results[`a',3] = 1
	mat results[`a',4] = `b'
	local ++a
	local ++b
	}
mat2txt, matrix(results) saving(fig3a) replace 

*** Generate data for Figure 2 (Immigration Attitudes for Democrats, Study 1) 

set more off
mat results = J(1,4,0)
local a=1
local b=1
foreach var of varlist r_immR{
reg `var' treat if rep == 0, robust
	mat results[`a',1] = _b[treat]
	mat results[`a',2] = _se[treat]
	mat results[`a',3] = `a'
	mat results[`a',3] = 1
	mat results[`a',4] = `b'
	local ++a
	local ++b
	}
mat2txt, matrix(results) saving(fig4a) replace 


*** Study 1 Immigration Attitudes (mentioned in the text): 

use "Study1_clean.dta", clear

ttest r_immR if rep == 0, by(treat)
ttest r_immR if rep == 1, by(treat)
ttest r_immR if treat !=., by(rep)



*** Generate Data for Figure 2 (Immigration Attutidues for Republicans, Study 2)

use "Study2_clean.dta", clear


set more off
mat results = J(18,4,0)
local a=1
local b=1
foreach var of varlist immR wallR2 uacR hawkeyeR legalR immig_index{
reg `var' treat22 treat23 treat24 trump if rep == 1, robust
	mat results[`a',1] = _b[treat22]
	mat results[`a',2] = _se[treat22]
	mat results[`a',3] = `a'
	mat results[`a',3] = 1
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat23]
	mat results[`a',2] = _se[treat23]
	mat results[`a',3] = `a'
	mat results[`a',3] = 2
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat24]
	mat results[`a',2] = _se[treat24]
	mat results[`a',3] = `a'
	mat results[`a',3] = 3
	mat results[`a',4] = `b'
	local ++a
	local ++b
	}
mat2txt, matrix(results) saving(fig3) replace 


*** Generate Data for Figure 2 (Immigration Attutidues for Democrats, Study 2)

set more off
mat results = J(18,4,0)
local a=1
local b=1
foreach var of varlist immR wallR2 uacR hawkeyeR legalR immig_index{
reg `var' treat22 treat23 treat24 trump if rep == 0, robust
	mat results[`a',1] = _b[treat22]
	mat results[`a',2] = _se[treat22]
	mat results[`a',3] = `a'
	mat results[`a',3] = 1
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat23]
	mat results[`a',2] = _se[treat23]
	mat results[`a',3] = `a'
	mat results[`a',3] = 2
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat24]
	mat results[`a',2] = _se[treat24]
	mat results[`a',3] = `a'
	mat results[`a',3] = 3
	mat results[`a',4] = `b'
	local ++a
	local ++b
	}
mat2txt, matrix(results) saving(fig4) replace


*** Calculate Cronbach's Alpha for Immigration Index (mentioned in text) 

alpha immR wallR2 uacR legalR hawkeyeR 
/*
Average interitem covariance:     .0774454
Number of items in the scale:            5
Scale reliability coefficient:      0.8600
*/
factor immR wallR2 uacR legalR hawkeyeR 
rotate



*** Generating Data for Figure 3 (Explanations of Immigration Attitudes for Republicans, Study 2) 

set more off
mat results = J(15,4,0)
local a=1
local b=1
foreach var of varlist econ_threa2 cultl_thre2 outgroupr ingroupr victimr{
reg `var' treat22 treat23 treat24 trump if rep == 1, robust
	mat results[`a',1] = _b[treat22]
	mat results[`a',2] = _se[treat22]
	mat results[`a',3] = `a'
	mat results[`a',3] = 1
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat23]
	mat results[`a',2] = _se[treat23]
	mat results[`a',3] = `a'
	mat results[`a',3] = 2
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat24]
	mat results[`a',2] = _se[treat24]
	mat results[`a',3] = `a'
	mat results[`a',3] = 3
	mat results[`a',4] = `b'
	local ++a
	local ++b
	}
mat2txt, matrix(results) saving(fig5) replace 


*** Generating Data for Figure 3 (Explanations of Immigration Attitudes for Democrats, Study 2) 

set more off
mat results = J(15,4,0)
local a=1
local b=1
foreach var of varlist econ_threa2 cultl_thre2 outgroupr ingroupr victimr{
reg `var' treat22 treat23 treat24 trump if rep == 0, robust
	mat results[`a',1] = _b[treat22]
	mat results[`a',2] = _se[treat22]
	mat results[`a',3] = `a'
	mat results[`a',3] = 1
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat23]
	mat results[`a',2] = _se[treat23]
	mat results[`a',3] = `a'
	mat results[`a',3] = 2
	mat results[`a',4] = `b'
	local ++a
	mat results[`a',1] = _b[treat24]
	mat results[`a',2] = _se[treat24]
	mat results[`a',3] = `a'
	mat results[`a',3] = 3
	mat results[`a',4] = `b'
	local ++a
	local ++b
	}
mat2txt, matrix(results) saving(fig6) replace 



